package com.iltgcl.echovoicedemo.data

import android.app.Application
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.iltgcl.echovoicedemo.R
import com.iltgcl.echovoicedemo.utils.SharedPreferencesUtils
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SettingsRepository @Inject constructor(application: Application) {
    private val contentResolver = application.contentResolver
    private val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application)
    private val resources = application.resources

    fun getVoiceName(): Int {
        return SharedPreferencesUtils.getIntFromStringPref(
            sharedPreferences, resources,
            R.string.pref_voice_name_key, R.string.pref_voice_name_default
        )
    }

    fun getSpeechRate(): Int {
        return SharedPreferencesUtils.getIntFromStringPref(
            sharedPreferences, resources,
            R.string.pref_speech_speed_key, R.string.pref_speech_speed_default
        )
    }

    fun getVolume(): Int {
        return SharedPreferencesUtils.getIntFromStringPref(
            sharedPreferences, resources,
            R.string.pref_speech_volume_key, R.string.pref_speech_volume_default
        )
    }

    fun getPitch(): Int {
        return SharedPreferencesUtils.getIntFromStringPref(
            sharedPreferences, resources,
            R.string.pref_speech_pitch_key, R.string.pref_speech_pitch_default
        )
    }

    fun getMediaFocusLossPolicy(): Int {
        return SharedPreferencesUtils.getIntFromStringPref(
            sharedPreferences, resources,
            R.string.pref_media_focus_loss_policy_key, R.string.pref_media_focus_loss_policy_default
        )
    }

    fun registerOnSharedPreferenceChangeListener(listener: SharedPreferences.OnSharedPreferenceChangeListener) {
        sharedPreferences.registerOnSharedPreferenceChangeListener(listener)
    }
    fun unregisterOnSharedPreferenceChangeListener(listener: SharedPreferences.OnSharedPreferenceChangeListener) {
        sharedPreferences.unregisterOnSharedPreferenceChangeListener(listener)
    }
}